<?php
namespace Modules\Comment\Entity;

use Core\Mvc\Entity\Entity;

class Comment extends Entity
{
    protected $_entity = 'comment';
    protected $_module = 'comment';
    protected $_relationships = [
        'node' => [
            'type' => 'hasOne',
            'key' => 'nid',
            'modelKey' => '_id',
            'obid' => true,
            'model' => '\Modules\Node\Entity\Node'
        ],
        'user' => [
            'type' => 'hasOne',
            'key' => 'uid',
            'modelKey' => '_id',
            'obid' => true,
            'model' => '\Modules\User\Entity\User'
        ]
    ];
    public $nid;
    public $pid;
    public $uid;
    public $anonymous;
    public $love = 0;
    public $commentNum = 0;
    public $hot = 0;

    public function beforeSave()
    {
        $this->nid = strval($this->nid);
        $this->pid = strval($this->pid);
        $this->love = intval($this->love);
        $this->commentNum = intval($this->commentNum);
        $this->hot = intval($this->love + $this->commentNum);
        if($this->user->isLogin()){
            $this->uid = $this->user->getUid();
        }else{
            $this->anonymous = $this->user->getUid();
        }
    }

    public function getLinks()
    {
        if (!$this->_links) {
            $this->_links = [
                'edit' => [
                    'href' => [
                        'for' => 'adminEntityEdit',
                        'entity' => 'comment',
                        'id' => $this->getId(),
                    ],
                    'data-target' => 'main',
                    'icon' => 'info',
                    'name' => '编辑',
                ],
                'delete' => [
                    'href' => [
                        'for' => 'adminEntityDelete',
                        'entity' => 'comment',
                        'id' => $this->getId(),
                    ],
                    'data-target' => 'main',
                    'icon' => 'danger',
                    'name' => '删除',
                ],
            ];
        }
        return parent::getLinks();
    }
}
